/*
 * Copyright (C) 2003, Velocityme Partnership
 * ProjectSessionBean.java
 *
 * Created on 29 April 2003, 08:22
 */

package com.velocityme.session;

import com.velocityme.entity.ChangeDeltaItemTypeBean;
import com.velocityme.entity.ChangeDeltaTypeBean;
import com.velocityme.entity.PermissionBean;
import javax.ejb.*;
import javax.naming.*;
import java.util.*;
import com.velocityme.interfaces.*;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.utility.PermissionDeniedException;
import com.velocityme.valueobjects.*;
import java.rmi.RemoteException;

/**
 *
 * @author  Robert Crida Work
 * @ejb.bean
 *           type="Stateless"
 *           cmp-version="2.x"
 *           name="ProjectSession"
 *           jndi-name="ejb/ProjectSession"
 *           view-type="local"
 *           transaction-type="Container"
 * @ejb.transaction type="Required"
 *
 * @ejb.util generate="physical"
 */
public class ProjectSessionBean implements SessionBean {
    
    private SessionContext m_context;
    
    private SearchSessionLocal m_searchSessionLocal;

    private PermissionLocal m_permissionProjectCreate;
    private PermissionLocal m_permissionProjectEdit;
    private PermissionLocal m_permissionNodeView;

    /**
     * If the user has permission to create a new Project in the specified parent
     * node then go ahead and create it and log an appropriate ChangeDelta.
     *
     * @param p_key User's security key
     * @param p_parentNode The node which will contain the new Project
     * @param p_projectValue The value details for the Project part of the person
     *
     * @ejb.interface-method view-type="local"
     */
    public ProjectLocal createProject(KeySessionLocal p_keyLocal,
                                      NodeLocal p_parentNodeLocal,
                                      com.velocityme.valueobjects.ProjectValue p_projectValue)
        throws PermissionDeniedException, InvalidKeyException {
        try {
            if (m_searchSessionLocal.getAssignedPermissionsLocal(p_keyLocal, p_parentNodeLocal).contains(m_permissionProjectCreate)) {
                DBObjectLocal dBObjectLocal = DBObjectUtil.getLocalHome().create();
                TaskValue taskValue = p_projectValue.getTaskValue();
                taskValue.setType("Project");
                taskValue.getNodeValue().setFinalType("Project");
                ChangeDeltaLocal changeDeltaLocal = ChangeDeltaUtil.getLocalHome().create(new Date(), ChangeDeltaTypeBean.CREATE, p_keyLocal.getUserLocal(), dBObjectLocal);
                TaskLocal taskLocal = TaskSessionUtil.getLocalHome().create().createTaskHelper(p_keyLocal, p_parentNodeLocal, taskValue, changeDeltaLocal);
                ProjectLocal projectLocal = ProjectUtil.getLocalHome().create(taskLocal, p_projectValue);
                changeDeltaLocal.addItem(ChangeDeltaItemTypeBean.SPONSOR, projectLocal.getSponsorPersonLocal().getName());
                changeDeltaLocal.addItem(ChangeDeltaItemTypeBean.PRIMARY_CONTACT, projectLocal.getPrimaryContactPersonLocal().getName());
                return projectLocal;
            }
            else
                throw new PermissionDeniedException();
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(CreateException e) {
            throw new EJBException(e);
        }
    }
    
    /**
     * If the user has permission to edit the project then do so and log the
     * changes with an appropriate ChangeDelta.
     *
     * @param p_key User's security key
     * @param p_project The Project object being editted
     * @param p_projectValue The value details for the Project part of the project
     *
     * @ejb.interface-method view-type="local"
     **/
    public void editProject(KeySessionLocal p_keyLocal,
                            ProjectLocal p_projectLocal,
                            com.velocityme.valueobjects.ProjectValue p_projectValue,
                            java.util.Collection p_responsibleContactableLocals,
                            java.util.Collection p_interestedContactableLocals)
        throws PermissionDeniedException, InvalidKeyException {
        try {
            TaskLocal taskLocal = p_projectLocal.getTaskLocal();
            NodeLocal nodeLocal = taskLocal.getNodeLocal();
            if (m_searchSessionLocal.getAssignedPermissionsLocal(p_keyLocal, nodeLocal).contains(m_permissionProjectEdit)) {
                TaskValue taskValue = p_projectValue.getTaskValue();
                ChangeDeltaLocal changeDeltaLocal = ChangeDeltaUtil.getLocalHome().create(new Date(), ChangeDeltaTypeBean.EDIT, p_keyLocal.getUserLocal(), nodeLocal.getDBObjectLocal());
                TaskSessionUtil.getLocalHome().create().editTaskHelper(p_keyLocal, taskLocal, taskValue, p_responsibleContactableLocals, p_interestedContactableLocals, changeDeltaLocal);
                Integer oldSponsorId = p_projectLocal.getSponsorPersonLocal().getPersonId();
                Integer oldPrimaryContactId = p_projectLocal.getPrimaryContactPersonLocal().getPersonId();
                p_projectLocal.setProjectValue(p_projectValue);
                if (!p_projectLocal.getSponsorPersonLocal().getPersonId().equals(oldSponsorId))
                    changeDeltaLocal.addItem(ChangeDeltaItemTypeBean.SPONSOR, p_projectLocal.getSponsorPersonLocal().getContactableLocal().getNodeLocal().getName());
                if (!p_projectLocal.getPrimaryContactPersonLocal().getPersonId().equals(oldPrimaryContactId))
                    changeDeltaLocal.addItem(ChangeDeltaItemTypeBean.PRIMARY_CONTACT, p_projectLocal.getPrimaryContactPersonLocal().getContactableLocal().getNodeLocal().getName());
            }
            else
                throw new PermissionDeniedException();
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(CreateException e) {
            throw new EJBException(e);
        }
    }
    
    /**
     * If the user has permission to access a project then return the local object.
     *
     * @param p_key User's security key
     * @param p_projectPK The Project object to lookup
     *
     * @ejb.interface-method view-type="local"
     **/
    public ProjectLocal getProjectLocal(KeySessionLocal p_keyLocal, 
                                        ProjectPK p_projectPK)
        throws PermissionDeniedException, InvalidKeyException {
        try {
            ProjectLocal projectLocal = ProjectUtil.getLocalHome().findByPrimaryKey(p_projectPK);
            NodeLocal nodeLocal = projectLocal.getTaskLocal().getNodeLocal();
            if (m_searchSessionLocal.getAssignedPermissionsLocal(p_keyLocal, nodeLocal).contains(m_permissionNodeView)) {
                return projectLocal;
            }
            else
                throw new PermissionDeniedException();
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(FinderException e) {
            throw new EJBException(e);
        }
    }
    
    /**
     * Create the Session Bean.
     * @throws CreateException 
     */
    public void ejbCreate() throws CreateException {
        try {
            m_searchSessionLocal = SearchSessionUtil.getLocalHome().create();
            m_permissionProjectCreate = PermissionUtil.getLocalHome().findByPrimaryKey(new PermissionPK(new Integer(PermissionBean.PROJECT_CREATE)));
            m_permissionProjectEdit = PermissionUtil.getLocalHome().findByPrimaryKey(new PermissionPK(new Integer(PermissionBean.PROJECT_EDIT)));
            m_permissionNodeView = PermissionUtil.getLocalHome().findByPrimaryKey(new PermissionPK(new Integer(PermissionBean.NODE_VIEW)));
        }
        catch(NamingException e) {
            throw new CreateException(e.getMessage());
        }
        catch(FinderException e) {
            throw new CreateException(e.getMessage());
        }
    }

    public void ejbActivate() throws java.rmi.RemoteException {
    }
    
    public void ejbPassivate() throws java.rmi.RemoteException {
    }
    
    public void ejbRemove() throws java.rmi.RemoteException {
    }
    
    public void setSessionContext(javax.ejb.SessionContext sessionContext) throws java.rmi.RemoteException {
        m_context = sessionContext;
    }
    
}
